<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_yxgz/model')->方法名();
*/
namespace jy_yxgz;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	public function checkworker(){
		if(!$_SESSION['workerid']){
			$_SESSION['workerid']=pdo_getcolumn('jy_yxw_worker',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'id');
			if(!$_SESSION['workerid']){
				exi('您还不是员工','','jy_yxgz/login/');
			}
		}
		$worker=pdo_get('jy_yxw_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['workerid']));
		if(!$worker['id']){
			exi('您还不是员工！','error','jy_yxgz/login/');
		}
		return $worker;
	}
	public function upfeelog($allkaoqingday=0,$type=0,$workerid=0){
		if($type && !$workerid){
			exi('请选择员工','error');
		}
		$month = date('Y-m',strtotime('-1 month'));
		$params=array('status'=>array(1,2),'uniacid'=>$_SESSION['uniacid']);
		if($type && $workerid){
			$params['id']=$workerid;
		}
		$workers=pdo_getall('jy_yxw_worker',$params);
		foreach($workers as $wv){
			if(!$wv['juese']){
				exi($wv['title'].'-不存在【职位】，无法生成薪资！','error');
			}
			$group2=pdo_get('jy_yxgz_feeset',array('uniacid'=>$_SESSION['uniacid'],'id'=>$wv['juese']));
			if(!$group2){
				exi($wv['title'].'-不存在【职位】，无法生成薪资！','error');
			}
			$group2['shifeeset']=json_decode($group2['shifeeset'],true);
			$group2['feeset']=json_decode($group2['feeset'],true);
			$feeinfo=$wv['status']==2?$group2['feeset']:$group2['shifeeset'];
			if(!$group2['role']){
				unset($feeinfo['bmyjfeeset']);
			}
			$feelog['role']=$group2['role'];
			$feelog['workerid']=$wv['id'];
			if($feelog['role']){
				$feelog['workerids']=pdo_getall('jy_yxw_worker',array('groupid'=>$wv['groupid'],'status'=>array(1,2),'uniacid'=>$_SESSION['uniacid']),'id','id');
				$feelog['workerids']=$feelog['workerids']?array_keys($feelog['workerids']):-1;
			}
			$feelog['month']=$month;
			$feeinfo=$this->getfee($feeinfo,$feelog);
			$data=array(
				'fee'=>$feeinfo['newfee'],
				'payfee'=>$feeinfo['newpayfee'],
				'feeinfo'=>ijson_encode($feeinfo),
			);
			$islog=pdo_get('jy_yxgz_feelog',array('month'=>$month,'uniacid'=>$_SESSION['uniacid'],'workerid'=>$wv['id']));
			$id=intval($islog['id']);
			if($id){
				$this->update('feelog',$data,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
			}else{
				$allkaoqingday = $allkaoqingday?:$this->getkqdays($month);
				$data['uniacid']=$_SESSION['uniacid'];
				$data['workerid']=$wv['id'];
				$data['juese']=$wv['juese'];
				$data['month']=$month;
				$data['allkaoqingday']=$allkaoqingday;
				$data['sn']='SN'.get_ordersn('jy_yxgz_feelog','sn');
				$data['createtime']=time();
				$this->insert('feelog',$data);
			}
		}
		return true;
	}
	public function getfee($feeinfo=array(),$feelog=array()){
		$feeinfo['newkqfee']=0;
		if($feeinfo['kqfee']>0 && $feelog['kaoqingday']>=$feelog['allkaoqingday'] && $feelog['allkaoqingday']>0){
			$feeinfo['newkqfee']=$feeinfo['kqfee'];
		}
		if($feelog['workerid'] && $feelog['month']){
			$month=str_array($feelog['month'],'-');
			$m=$month[1];
			$y=$month[0];
			$yuestart = mktime(0,0,0,$m,1,$y);
			$yueend = mktime(0,0,0,intval($m)+1,1,$y);
			$yueend -=100;
			$yefee=pdo_getsum('jy_yxgz_orderlog',array('status'=>1,'workerid'=>$feelog['workerid'],'uniacid'=>$_SESSION['uniacid'],'pay_time between'=>array($yuestart,$yueend)),'price');
			if($this->setting['yeji_type']){
				$benprice=pdo_getsum('jy_yxgz_orderlog',array('status'=>1,'workerid'=>$feelog['workerid'],'uniacid'=>$_SESSION['uniacid'],'pay_time between'=>array($yuestart,$yueend)),'benprice');
				$yefee -=$benprice;
			}
			$feeinfo['newbzfee']=0;
			if(($yefee>=$feeinfo['bzmanfee'] || $feelog['kaoqingday']>=$feelog['allkaoqingday']) && $feeinfo['bzfee']>0){
				$feeinfo['newbzfee']=$feeinfo['bzfee'];
			}
			$feeinfo['yeji_type']=$this->setting['yeji_type'];
			$feeinfo['allyefee']=$yefee;
			$feeinfo['newyjfee']=$this->getyjfee($feeinfo,$yefee);
			if($feelog['role']){
				$bmyefee=pdo_getsum('jy_yxgz_orderlog',array('status'=>1,'workerid'=>$feelog['workerids'],'uniacid'=>$_SESSION['uniacid'],'pay_time between'=>array($yuestart,$yueend)),'price');
				$benprice=pdo_getsum('jy_yxgz_orderlog',array('status'=>1,'workerid'=>$feelog['workerids'],'uniacid'=>$_SESSION['uniacid'],'pay_time between'=>array($yuestart,$yueend)),'benprice');
				if($this->setting['yeji_type']){
					$bmyefee -=$benprice;
				}
				$feeinfo['newbmyjfee']=$this->getbmyjfee($feeinfo,$bmyefee);
				$feeinfo['newbmtcfee']=$this->getbmtcfee($feeinfo,$bmyefee);
			}else{
				$feeinfo['newbmyjfee']=0;
				$feeinfo['newbmtcfee']=0;
			}
			$feeinfo['newtcfee']=$this->gettcfee($feeinfo,$yefee);
		}
		$she_rate=$this->setting['she_rate']?:30;
		$gj_rate=$this->setting['gj_rate']?:30;
		$jifee=intval($feeinfo['jifee']);
		if($feelog['kaoqingday']){
			$feelog['allkaoqingday']=$feelog['allkaoqingday']?:22;
			$dayjifee=intval($feeinfo['jifee'])/$feelog['allkaoqingday'];
			if($feelog['kaoqingday']>$feelog['allkaoqingday']){
				$gj_rate=$this->setting['gj_rate']?:1;
				$fee_jiabei=$gj_rate*$dayjifee*($feelog['kaoqingday']-$feelog['allkaoqingday']);
				$jifee +=$fee_jiabei;
			}
			if($feelog['kaoqingday']<$feelog['allkaoqingday']){
				$jifee=$feelog['kaoqingday']*$dayjifee;
			}
			$jifee=price($jifee,'int');
		}
		$feeinfo['bufee']=intval($feeinfo['bufee']);
		$feeinfo['newgzfee']=$jifee+$feeinfo['newkqfee']+$feeinfo['newbzfee']+$feeinfo['newyjfee']+$feeinfo['newtcfee']+$feeinfo['bufee'];
		$feeinfo['newshefee']=price($feeinfo['she1fee'],'int')+price($feeinfo['she2fee'],'int')+price($feeinfo['she3fee'],'int');
		$feeinfo['newkoushefee']=$feeinfo['newshefee']*$she_rate/100;
		$feeinfo['newfee']=$feeinfo['newgzfee']-$feeinfo['newkoushefee'];
		$feeinfo['newkougjjfee']=$feeinfo['gjjfee']*$gj_rate/100;
		$feeinfo['newfee']=$feeinfo['newfee']-$feeinfo['newkougjjfee'];
		$feeinfo['newsuifee']=$feeinfo['newsuifee']?:0;
		$feeinfo['newpayfee']=$feeinfo['newfee']-$feeinfo['newsuifee'];
		if($feelog['workerid'] && $feelog['month']){
			$feelog['morekoufee']=pdo_getsum('jy_yxgz_morefeelog',array('isadd'=>0,'workerid'=>$feelog['workerid'],'uniacid'=>$_SESSION['uniacid'],'month'=>$feelog['month']),'price');
			if($feelog['morekoufee']){
				$feeinfo['newpayfee'] -=$feelog['morekoufee'];
			}
			$feelog['moreaddfee']=pdo_getsum('jy_yxgz_morefeelog',array('isadd'=>1,'workerid'=>$feelog['workerid'],'uniacid'=>$_SESSION['uniacid'],'month'=>$feelog['month']),'price');
			if($feelog['moreaddfee']){
				$feeinfo['newpayfee'] +=$feelog['moreaddfee'];
			}
		}
		return $feeinfo;
	}
	public function gettcfee($feeinfo=array(),$yefee=0){
		$newtcfee=0;
		paixu($feeinfo['tcfeeset'],'man','desc');
		foreach($feeinfo['tcfeeset'] as $fv){
			if($yefee>=$fv['min'] && $yefee<=$fv['max']){
				$newtcfee=$fv['rate']*$yefee/100;
				break;
			}
		}
		return $newtcfee;
	}
	public function getyjfee($feeinfo=array(),$yefee=0){
		$newyjfee=0;
		paixu($feeinfo['yjfeeset'],'man','desc');
		foreach($feeinfo['yjfeeset'] as $fv){
			if($yefee>=$fv['man']){
				$newyjfee=$fv['fee'];
				break;
			}
		}
		return $newyjfee;
	}
	public function getbmtcfee($feeinfo=array(),$yefee=0){
		$newtcfee=0;
		paixu($feeinfo['bmtcfeeset'],'man','desc');
		foreach($feeinfo['bmtcfeeset'] as $fv){
			if($yefee>=$fv['min'] && $yefee<=$fv['max']){
				$newtcfee=$fv['rate']*$yefee/100;
				break;
			}
		}
		return $newtcfee;
	}
	public function getbmyjfee($feeinfo=array(),$yefee=0){
		$newyjfee=0;
		paixu($feeinfo['bmyjfeeset'],'man','desc');
		foreach($feeinfo['bmyjfeeset'] as $fv){
			if($yefee>=$fv['man']){
				$newyjfee=$fv['fee'];
				break;
			}
		}
		return $newyjfee;
	}
	public function getkqdays($month=''){
		$daynum = date("t",strtotime($month));
		$allkaoqingday=$daynum;
		$i = 1;
		while ($i <= $daynum){
			if ($i < 10) {
				$i2 = '0' . $i;
			}else{
				$i2=$i;
			}
			$daydata=$month.'-'.$i2;
			$weekid=date("w",strtotime($daydata));
			if(!$weekid || $weekid==6){
				$allkaoqingday--;
			}
			++$i;
		}
		return $allkaoqingday;
	}
}
?>